package com.motong.gongdan.vo.system.otherVo;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.motong.gongdan.vo.system.common.query.SecondGroupCondition;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author:fff
 * @time:2022/2/28
 */
@Data
public class WorkOrderListVo extends PageVo {
    private String code;
    private String productCode;
    private String productName;
    private String productSpecification;
    private String unit;
    private String comment;
    private BigDecimal planedNumberDown;
    private BigDecimal planedNumberUp;
    private BigDecimal actualNumberDown;
    private BigDecimal actualNumberUp;
    private BigDecimal goodProductNumberDown;
    private BigDecimal goodProductNumberUp;
    private BigDecimal badProductNumberDown;
    private BigDecimal badProductNumberUp;
    private Date startTimeStart;
    private Date startTimeEnd;
    private Date endTimeStart;
    private Date endTimeEnd;
    private Date actualStartTimeStart;
    private Date actualStartTimeEnd;
    private Date actualEndTimeStart;
    private Date actualEndTimeEnd;
    private Integer isUrgent;
    private List<Integer> state;

    @ApiModelProperty("筛选方案自定义字段查询")
    private Map<Integer, String> customFieldMap;

    @ApiModelProperty("表单自定义字段查询")
    private JSONObject searchOther;

    @ApiModelProperty("看板id")
    private Integer screenPlanId;

    @ApiModelProperty(value = "工单id", hidden = true)
    List<Integer> ids = new ArrayList<>();

    @ApiModelProperty("单位id集合")
    private List<String> unitIdList = Collections.emptyList();

    @ApiModelProperty("排序相关信息")
    private List<SortInfo> sortInfoList = Collections.emptyList();

    @ApiModelProperty(value = "排序信息", hidden = true)
    private String sortInfoStr = "a.create_time DESC";

    @ApiModelProperty("二级分组条件")
    private SecondGroupCondition secondSearchCondition;


    public void setUnit(String unit) {
        if (StringUtils.hasLength(unit)) {
            this.unitIdList = Arrays.asList(unit.split(","));
        }
        this.unit = null;
    }


    public void setSortInfoList(List<SortInfo> sortInfoList) {
        if (!CollectionUtils.isEmpty(sortInfoList)) {
            this.sortInfoStr = sortInfoList.stream().filter(i -> StringUtils.hasLength(i.getOrderBy()) && StringUtils.hasLength(i.getSort()))
                    .map(i -> i.getOrderBy().concat(" ".concat(i.getSort()))).collect(Collectors.joining(","));
            if (!StringUtils.hasLength(this.sortInfoStr)) {
                this.sortInfoStr = "w.create_time DESC";
            }

            this.sortInfoList = sortInfoList;
        }
    }


    @Data
    @Accessors(chain = true)
    public static class SortInfo {
        @ApiModelProperty("排序列(创建时间 createTime, 更新时间 updateTime ,完成进度 finishProgress)")
        private String orderBy;

        @ApiModelProperty("排序(ASC 升序, DESC 降序)")
        private String sort;


        public void setSort(String sort) {
            List<String> columnList = Arrays.asList("DESC", "ASC");
            if (columnList.contains(sort)) {
                this.sort = sort;
            }
        }

        public void setOrderBy(String orderBy) {
            List<String> columnList = Arrays.asList("createTime", "updateTime", "finishProgress", "fnishProgress");
            if (columnList.contains(orderBy)) {
                this.orderBy = "finishProgress".equals(orderBy) || "fnishProgress".equals(orderBy) ? "allProcessRate" : "a.".concat(StrUtil.toUnderlineCase(orderBy));
            }
        }
    }

}
